<?php
namespace app\redbag\model;

use app\BaseModel;
use think\facade\Db;

class AdminActive extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_redbag_active';

    protected $append = [

      'create_user_text'
    ];


    /**
     * @author chenniang
     * @DataTime: 2020-04-09 16:02
     * @功能说明:发布者
     */
    public function getCreateUserTextAttr($value,$data){

        if(!empty($data['create_user'])){

            $name = Db::name('longbing_card_user_info')->where(['fans_id'=>$data['create_user']])->value('name');

            if(empty($name)){

                $name = Db::name('longbing_card_user_info')->where(['id'=>$data['create_user']])->value('nickName');
            }
            return $name.'个人发布';
        }else{

            return '公司发布';
        }

    }


    /**
     * @param $value
     * @param $data
     * @功能说明:转换图片格式
     * @author chenniang
     * @DataTime: 2020-03-09 18:56
     */
    public function getImgsAttr($value,$data){

        $img = transImagesOne([$value],['imgs'],$data['uniacid']);

        return !empty($img[0])?$img[0]:'';

    }


    /**
     * @param $value
     * @功能说明:封面图
     * @author chenniang
     * @DataTime: 2020-03-09 19:02
     */
    public function getCoverAttr($value,$data){

        $img = transImagesOne([$value],['cover'],$data['uniacid']);

        return !empty($img[0])?$img[0]:'';
    }

    /**
     * @param $dis
     * @param $page
     * @功能说明:活动列表
     * @author chenniang
     * @DataTime: 2020-03-09 18:42
     */

    public function activeList($dis,$page){

        $data = $this->where($dis)->order(['id desc'])->paginate($page)->toArray();

        if(!empty($data['data'])){
            //记录模型
            $record_model  = new Record();
            //记录模型
            $staff_record = new staffRecord();

            foreach ($data['data'] as &$v){
                //总数
                $count  = $staff_record->where(['active_id'=>$v['id']])->count();
                //红包总数
                $v['total_num']  = $count*$v['num'];
                //总金额
                $v['total_price']= round($v['total_num']*$v['price'],2);

                $v['price']  = round($v['price'],2);
                //已经使用的数量
                $record_dis = [
                    //活动id
                    'active_id' => $v['id'],
                    //状态
                    'status'    => 1
                ];
                $use_count   = $record_model->where($record_dis)->count();
                //剩余数量
                $v['y_num']  = $v['total_num'] - $use_count;
                //剩余金额
                $v['y_price']= round($v['y_num']*$v['price'],2);
            }
        }
        return $data;
    }


    /**
     * User: chenniang
     * Date: 2019-12-18 14:11
     * @return void
     * descrption:活动详情
     */
    public function activeInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];
    }

    /**
     * User: chenniang
     * Date: 2019-12-18 14:11
     * @return void
     * descrption:添加活动
     */
    public function activeAdd($data){
        //开始时间
        $data['create_time'] = time();
        //更新时间
        $data['update_time'] = time();
        //状态
        $data['status']      = 1;
        //赋值了就删掉
        if(isset($data['company'])){

            $company = $data['company'];

            unset($data['company']);
        }
        //指定员工
        if(isset($data['staff'])){

            $staff = $data['staff'];

            unset($data['staff']);
        }
        Db::startTrans();
        $res = $this->insert($data);
        //获取id
        $id  = $this->getLastInsID();
        //公司
        $company_model = new AdminCompany();

        //添加到每个公司
        if(!empty($company)){

            foreach ($company as $v){

                $insert = [
                    //uniacid
                    'uniacid'    => $data['uniacid'],
                    //公司id
                    'company_id' => $v,
                    //活动id
                    'active_id'  => $id
                ];

               $res = $company_model->companyAdd($insert);
            }
        }
        //指定员工
        if(!empty($staff)){

            foreach ($staff as $v){

                $insert = [
                    //uniacid
                    'uniacid'    => $data['uniacid'],
                    //员工id
                    'staff_id'   => $v,
                    //活动id
                    'active_id'  => $id
                ];
                $res = $company_model->companyAdd($insert);
            }
        }
        //添加到记录列表
        $this->addRecord($data,$company,$staff,$id);

        Db::commit();
        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-11 09:47
     * @功能说明:添加到记录列表
     */
    public function addRecord($data,$company,$staff,$id){

        if($data['user_type']==0){
            //如果不限制
            $staffs = Db::name('longbing_card_user_info')->where(['uniacid'=>$data['uniacid'],'is_staff'=>1])->column('fans_id');

        }elseif($data['user_type']==1){
            //公司
            $staffs = Db::name('longbing_card_user_info')->where([['company_id','in',$company],['is_staff','=',1]])->column('fans_id');

        }else{
            //员工
            $staffs = $staff;
        }

        if(!empty($staffs)){
            //记录模型
            $staff_record = new staffRecord();

            foreach ($staffs as $k=>$v){

                $insert[$k]['uniacid']   = $data['uniacid'];

                $insert[$k]['active_id'] = $id;

                $insert[$k]['staff_id']  = $v;

                $insert[$k]['total_num'] = $data['num'];

                $insert[$k]['has_num']   = $data['num'];

                $insert[$k]['price']     = $data['price'];

                $insert[$k]['total_price'] = round($data['price']*$data['num'],2);

                $insert[$k]['create_time'] = time();

                $insert[$k]['update_time'] = time();

            }

            $staff_record->saveAll($insert);
        }

        return true;
    }

    /**
     * User: chenniang
     * Date: 2019-12-18 14:13
     * @return void
     * descrption:编辑活动
     */
    public function activeUpdate($dis,$data){
        //更新时间
        $data['update_time'] = time();

        //添加到每个公司
        if(!empty($data['company'])){
            //公司
            $company_model = new AdminCompany();
            //全部删除
            $company_model->companyDel(['active_id'=>$data['id']]);

            foreach ($data['company'] as $v){

                $insert = [
                    //uniacid
                    'uniacid'    => $data['uniacid'],
                    //公司id
                    'company_id' => $v,
                    //活动id
                    'active_id'  => $data['id']
                ];
                $res = $company_model->companyAdd($insert);
            }
            unset($data['company']);
        }
        $res = $this->where($dis)->update($data);

        return $res;
    }














}